// ignore_for_file: non_constant_identifier_names

import 'package:badges/badges.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:verse_systems/commom/global/global.dart';
import 'package:verse_systems/commom/provider/common_provider.dart';
import 'package:verse_systems/pages/home/home.dart';
import 'package:verse_systems/pages/home/home_control.dart';
import 'package:verse_systems/pages/service/service.dart';
import 'package:verse_systems/pages/mine/mine.dart';
import 'package:verse_systems/res/colors.dart';
import 'package:badges/badges.dart' as badges;

class TabbarPages extends StatefulWidget {
  const TabbarPages({super.key});

  @override
  State<TabbarPages> createState() => _TabbarPagesState();
}

class _TabbarPagesState extends State<TabbarPages> {
  final List<Widget> tabBody = [
    const HomePage(),
    const ServicePage(),
    const MinePage(),
  ];

  @override
  Widget build(BuildContext context) {
    int currentIndex = Provider.of<CommonProvider>(context).currentIndex;
    int message = Provider.of<CommonProvider>(context).message;
    return Scaffold(
      key: Global.scaffoldKey,
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        fixedColor: Colours.app_main_background_color,
        currentIndex: currentIndex,
        items: [
          BottomNavigationBarItem(
              icon: const Icon(CupertinoIcons.home), label: 'home_page'.tr),
          BottomNavigationBarItem(
            icon: badges.Badge(
              badgeStyle: BadgeStyle(
                  badgeColor: (message == 0 || currentIndex == 1)
                      ? Colors.transparent
                      : Colors.red),
              badgeContent: Text(
                message.toString(),
                style: TextStyle(
                    color: (message == 0 || currentIndex == 1)
                        ? Colors.transparent
                        : Colors.white),
              ),
              child: const Icon(CupertinoIcons.conversation_bubble),
            ),
            label: 'customer_page'.tr,
          ),
         BottomNavigationBarItem(
              icon: const Icon(CupertinoIcons.person), label: 'mine_page'.tr),
        ],
        onTap: (index) {
          Provider.of<CommonProvider>(context, listen: false)
              .changeIndex(index);
          debugPrint("点击table$index");
          switch (index) {
            //刷新首页
            case 0:
              {
                Global.isChat = false;
                final home_control = Get.find<HomeControl>();
                Future.delayed(const Duration(milliseconds: 500),
                    () => home_control.getAppHomeInfoList());
              }
              break;
            case 1:
              {
                Global.isChat = true;
                Global.count = 0;
                Provider.of<CommonProvider>(context, listen: false)
                    .addIndex(Global.count);
              }
              break;
            case 2:
              {
                Global.isChat = false;
              }
              break;
            default:
          }
        },
      ),
      body: IndexedStack(index: currentIndex, children: tabBody),
    );
  }
}
